package com.example.lean;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    TextView txt;

    ImageView img;
    private static final int item1 = Menu.FIRST;
    private static final int item2 = Menu.FIRST+1;
    private static final int item3 = Menu.FIRST+2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txt = findViewById(R.id.txt);

        registerForContextMenu(txt);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // 调用父类方法来加入系统菜单
        super.onCreateOptionsMenu(menu);
        // 添加菜单项
        menu.add(
                1,//组号
                1, //唯一的ID号
                1, //排序号
                "主页"); //标题
        menu.add( 1, 2, 2,  "搜索");
        menu.add( 1, 3, 3,  "设置");
        menu.add( 1, 4, 4,  "退出");
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        String title = "选择了" + item.getTitle().toString();
        switch (item.getItemId())
        { //响应每个菜单项(通过菜单项的ID)
            case 1:
                txt.setText(title);
                break;
            case 2:
                txt.setText(title);
                break;
            case 3:
                txt.setText(title);
                break;
            case 4:
                txt.setText(title);
                break;
            default:
                //对没有处理的事件，交给父类来处理
                return super.onOptionsItemSelected(item);
        }
        return true;
    }
    //上下文菜单，长按特定视图会激活上下文菜单
    @Override
    public void onCreateContextMenu(ContextMenu menu, View view,
                                    ContextMenu.ContextMenuInfo menuInfo) {
        //添加菜单项
        menu.add(0, item1, 0, "复制");
        menu.add(0, item2, 0, "收藏");
        menu.add(0, item3, 0, "提醒");
    }
    //菜单单击响应
    @Override
    public boolean onContextItemSelected(MenuItem item){
        String title = "选择了" + item.getTitle().toString();
        txt.setText(title);
        //获取当前被选择的菜单项的信息
        switch(item.getItemId())
        {
            case item1:
                //复制代码
                break;
            case item2:
                //收藏代码
                break;
            case item3:
                //提醒代码
                break;
        }
        return true;
    }
}